//
//  OMEditViewController.m
//  Weibbo
//
//  Created by 李 林刚 on 13-7-14.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "OMEditViewController.h"
#import "OMAttributeTextView.h"
#import<CoreText/CoreText.h>



@interface OMEditViewController ()<UITextViewDelegate>
@property (nonatomic, retain) OMAttributeTextView  *editTextView;
@property (nonatomic, retain) NSAttributedString   *attributeString;
@property (nonatomic, assign) NSInteger           row;

@end

@implementation OMEditViewController
@synthesize delegate;

@synthesize editTextView;
@synthesize attributeString;
@synthesize row;

-(void)dealloc
{
    UIRelease(editTextView);
    UIRelease(attributeString);
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithIndex:(NSInteger)index richText:(NSAttributedString *)text
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.attributeString = text;
        self.row = index;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    [paragraphStyle setLineSpacing:10];
//    [paragraphStyle setParagraphSpacing:5];
//    [paragraphStyle setFirstLineHeadIndent:20];
    self.editTextView = [[OMAttributeTextView alloc]initWithFrame:self.view.bounds];
    [self.editTextView setAllowsEditingTextAttributes:YES];
    
    UIToolbar  *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    
    NSMutableArray  *toolbarArray = [NSMutableArray array];
    
    
    for(int i = 0 ;i < 8; i++)
    {
        UIButton  *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setFrame:CGRectMake(0, 0, 30, 30)];
        [button setTag:i];
        [button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem  *item = [[UIBarButtonItem alloc] initWithCustomView:button];
        [toolbarArray addObject:item];
        
        if (i == 0) {
            [button setTitle:@"加粗" forState:UIControlStateNormal];
        }
        if (i == 1) {
            [button setTitle:@"倾斜" forState:UIControlStateNormal];
        }
        if (i == 2) {
            [button setTitle:@"下划" forState:UIControlStateNormal];
        }
        if (i == 3) {
            [button setTitle:@"高亮" forState:UIControlStateNormal];
        }
        if (i == 4) {
            [button setTitle:@"删除" forState:UIControlStateNormal];
        }
        if (i == 5) {
            [button setTitle:@"阴影" forState:UIControlStateNormal];
        }
        if (i == 6) {
            [button setTitle:@"返回" forState:UIControlStateNormal];
        }
        if (i == 7) {
            [button setTitle:@"颜色" forState:UIControlStateNormal];
        }
    }
    [self.editTextView setInputAccessoryView:toolBar];
    
    [toolBar setItems:toolbarArray];
    
    if ([self.attributeString length] == 0) {
        NSMutableAttributedString  *attri = [[[NSMutableAttributedString alloc] initWithString:@""] autorelease];
        self.attributeString = attri;
        [self.editTextView setAttributedText:self.attributeString];
        [self.editTextView setFont:UIFontOfSize(18.0f) range:NSMakeRange(0, [self.attributeString length])];
    }
    [self.editTextView setFont:UIFontOfSize(18.0f)];
    [self.editTextView setAttributedText:self.attributeString];
//    [self.editTextView setParagraphStyle:paragraphStyle];
    [self.editTextView setDelegate:self];
    [self.view addSubview:self.editTextView];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([self.editTextView canBecomeFirstResponder]) {
        [self.editTextView becomeFirstResponder];
    }
}

-(void)viewDidUnload
{
    UIRelease(editTextView);
    UIRelease(attributeString);
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)clicked:(UIButton *)sender
{
    NSRange selectRange = [self.editTextView selectedRange];
    NSLog(@"%@",NSStringFromRange(selectRange));
    int tag = [sender tag];
    switch (tag) {
        case 0:
            [self.editTextView setBoldForSelection];
            break;
        case 1:
            [self.editTextView setItalicForSelection];
            break;
        case 2:
            if ([self.editTextView isUnderLineForSelection]) {
                [self.editTextView removeUnderLine:selectRange];
            }else{
                [self.editTextView setUnderLine:selectRange];
            }
            break;
        case 3:
            if ([self.editTextView isHighLightForSelection]) {
                [self.editTextView removeHighlightColor:selectRange];
            }else{
                [self.editTextView setHighlightColor:[UIColor redColor] range:selectRange];
            }
            break;
        case 4:
            if ([self.editTextView isStrokeForSelection]) {
                [self.editTextView removeStroke:selectRange];
            }else{
                [self.editTextView setStroke:[UIColor blackColor] range:selectRange];
            }
            break;
        case 5:
        {
            if ([self.editTextView isShadowForSelection]) {
                [self.editTextView removeShawdow:selectRange];
            }else{
                NSShadow  *shadow = [[NSShadow alloc] init];
                [shadow setShadowColor:[UIColor redColor]];
                [shadow setShadowOffset:CGSizeMake(1, 1)];
                [shadow setShadowBlurRadius:1];
                [self.editTextView setShadow:shadow range:selectRange];
                [shadow release];
            }
            
        }
            break;
        case 6:
        {
//            [self.editTextView setFont:[UIFont systemFontOfSize:20.0f] range:selectRange];
            if ([delegate respondsToSelector:@selector(OMEditViewControllerDelegate:NSAttributedString:)]) {
                [delegate OMEditViewControllerDelegate:self.row NSAttributedString:self.editTextView.attributedText];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
            break;
        case 7:
            [self.editTextView setColor:[UIColor blueColor] range:selectRange];
            break;
        default:
            break;
    }
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //    int length = [[textView attributedText] length];
    //    if ([[textView text] hasSuffix:@"\n"] && [text isEqualToString:@"\n"]) {
    //        textView.selectedRange = NSMakeRange(length - 1, 0);
    //        return NO;
    //    }
    //    if (range.location < length - 1 && [text isEqualToString:@"\n"]) {
    //        return NO;
    //    }
    //    return  YES;
    return YES;
}

@end
